import {Api} from "../../../utils/api";
import Dialog from "@vant/weapp/dialog/dialog";
import http from "../../../utils/http";

Page({
    data: {
        companyInfo: {
            id: '',
            name: '',
            image: '',
            desc: ''
        },
        imageList: [],
    },
    onLoad: function (options) {

    },
    afterRead(event) {
        const { file } = event.detail;
        const token = wx.getStorageSync('token');
        const _this = this
        wx.uploadFile({
            url: Api.http + Api.uploadImage,
            filePath: file.url,
            name: 'file',
            formData: { type: 'company' , token},
            success(res) {
                const image = JSON.parse(res.data).data
                _this.setData({ 'companyInfo.image': image, imageList: [{url: image}] });
            },
        });
    },
    onOversize() {
        Dialog.alert({ title: '提示', message: '文件大小不能超过 2MB' });
    },
    onDeleteImage() {
        this.setData({ 'companyInfo.image': '', imageList: [] });
    },
    onChangeField(e) {
        console.log(e)
        const field = e.currentTarget.dataset.field
        const value = e.detail
        this.setData({[`companyInfo.${field}`]: value})
    },
    saveCompanyInfo() {
        const companyInfo = this.data.companyInfo
        if (!companyInfo.name) {
            Dialog.alert({ title: '提示', message: '公司名称不能为空' });
            return
        }
        http.postRequest(Api.saveCompanyInfo, companyInfo, (res) => {
            Dialog.alert({ title: '提示', message: '保存成功' }).then(() => {
                wx.navigateBack()
            })
        })
    }
});
